package com.hspedu.map_;

import java.util.*;

/**
 * 使用HashMap添加3个员工对象, 要求:
 * 键: 员工id
 * 值: 员工对象
 * 并遍历显示工资 >18000的员工(遍历方式最少两种)
 * 员工类: 姓名, 工资, 员工id
 */
public class MapExercise {
    public static void main(String[] args) {
        Employee e1 = new Employee("张三", 10000.0, 1);
        Employee e2 = new Employee("李四", 20000.0, 2);
        Employee e3 = new Employee("王五", 30000.0, 3);

        HashMap map = new HashMap();
        map.put(e1.getId(), e1);
        map.put(e2.getId(), e2);
        map.put(e3.getId(), e3);

        //遍历
        System.out.println("======取出value, 使用增强for遍历======");
        Collection values = map.values();
        for (Object value : values) {
            Employee e = (Employee) value;
            if (e.getSalary() > 18000){
                System.out.println(value);
            }
        }
        System.out.println("======取出value, 使用迭代器遍历======");
        Iterator iterator = values.iterator();
        while (iterator.hasNext()) {
            Object value = iterator.next();
            Employee e = (Employee) value;
            if (e.getSalary() > 18000){
                System.out.println(value);
            }
        }

        System.out.println("======取出key, 使用增强for遍历======");
        Set key = map.keySet();
        for (Object o : key) {
            Object o1 = map.get(o);
            Employee e = (Employee) o1;
            if (e.getSalary() > 18000){
                System.out.println(map.get(o));
            }
        }
        System.out.println("======取出key, 使用迭代器遍历======");
        Iterator iterator1 = key.iterator();
        while (iterator1.hasNext()) {
            Object o = iterator1.next();
            Object o1 = map.get(o);
            Employee e = (Employee) o1;
            if (e.getSalary() > 18000){
                System.out.println(map.get(o));
            }
        }

        System.out.println("========使用entrySet 的增强 for ========");
        Set entrySet = map.entrySet();
        for (Object entry : entrySet) {
            Map.Entry m = (Map.Entry) entry;
            Object value = m.getValue();
            Employee e = (Employee) value;
            if (e.getSalary() > 18000){
                System.out.println(m.getValue());
            }
        }
        System.out.println("========使用entrySet 的迭代器 ========");
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Object next = iterator2.next();
            Map.Entry m = (Map.Entry) next;
            Object value = m.getValue();
            Employee e = (Employee) value;
            if (e.getSalary() > 18000){
                System.out.println(m.getValue());
            }
        }
    }
}


class Employee{
    private String name;
    private double salary;
    private int id;

    public Employee(String name, double salary, int id) {
        this.name = name;
        this.salary = salary;
        this.id = id;
    }

    public Employee() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", id=" + id +
                '}';
    }
}